package org.itboys.mobile.service.mongo.mobile;

import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductCategoryMapper;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mongo.mobile.cookbook.KitchenMobileCookBookService;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：花果山_其他接口__service_移动端
 */
@Service
public class KitchenMobileCommonService {
    @Autowired
    private SundayMobileAdvertisementService advertisementService;
    @Autowired
    private SundayMobileProductService productService;
    @Autowired
    private KitchenMobileCookBookService cookBookService;
    @Autowired
    private SundayProductCategoryMapper productCategoryMapper;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private KitchenMobileVillageService villageService;
    /**
     * app首页接口
     * @param memberId
     * @return
     */
    public Map<String,Object> getIndexData(Long memberId,Long villageId){
        Map<String,Object> result = new HashMap<String,Object>();
        Map<String,Object> param = new HashMap<String,Object>();

        //用户小区
        List<Map<String,Object>> villages= villageService.getMemberVillage(memberId);
        result.put("villages",villages);



        String imgRoot = resourceHolder.getStringValue("imgRoot");
        //首页轮播图
        List<SundayAdvertisementDto> images =advertisementService.getAdvertisementByZone("index_roll");
        result.put("images",images);
        //首页新闻
        List<SundayAdvertisementDto> news =advertisementService.getAdvertisementByZone("index_news");
        result.put("news",news);
        //首页一级分类
        param.clear();
        param.put("parentId",0);
        param.put("sort","sequence");
        param.put("order","asc");
        List<SundayProductCategory>  categoryLists = productCategoryMapper.select(param);
        List<Map<String,Object>> categories =new ArrayList<Map<String, Object>>();
        for(SundayProductCategory categoryList:categoryLists){
            Map<String,Object> category = new HashMap<String,Object>();
            category.put("id",categoryList.getId());
            category.put("name",categoryList.getName());
            category.put("image",imgRoot+categoryList.getImage());

            categories.add(category);
        }
        result.put("categories",categories);
        //推荐食谱。只取10个
        param.clear();
        param.put("isDeleted",0);
        param.put("isIndexRecommend",1);

        List<Map<String,Object>> cookBooks =new ArrayList<Map<String, Object>>();

        List<KitchenCookBook> books=cookBookService.list(param,10);
        for(KitchenCookBook book:books){
            Map<String,Object> cookBook = new HashMap<String,Object>();
            cookBook.put("id",book.getId());
            cookBook.put("name",book.getName());
            cookBook.put("viceName",book.getViceName());
            cookBook.put("image",imgRoot+book.getImage());
            cookBooks.add(cookBook);
        }
        result.put("cookbooks",cookBooks);

        //推荐商品。只取10个
        param.clear();
        param.put("isDeleted",0);
        param.put("isIndexRecommend",1);
        param.put("row_start",0);
        param.put("row_size",10);
        param.put("type", SundayCommonConstants.type_mall);
        param.put("status",1);
        //需要根据用户获取不同的价格

          List<SundayProduct> ps=productService.getShopProduct(null,
                  param,
                  SundayCommonConstants.type_mall,
                  null,
                  memberId,
                  null,
                  villageId,true);
        List<Map<String,Object>> products =new ArrayList<Map<String, Object>>();
        for(SundayProductDto p:productService.packageDtoMuilt(ps)){
            Map<String,Object> product = new HashMap<String,Object>();
            product.put("id",p.getId());
            product.put("name",p.getName());
            product.put("standard",p.getStandard());
            product.put("image",p.getImage());
            product.put("marketPrice",p.getMarketPrice());
            product.put("price",p.getPrice());
            products.add(product);
        }
        result.put("products",products);
        return result;
    }


}
